package org.sith.taskrunner.beans.sum;

import java.io.Serializable;

import javax.ejb.EJB;

import org.sith.taskrunner.api.task.Task;
import org.sith.taskrunner.api.task.TaskExecutionResult;
import org.sith.taskrunner.api.task.TaskInputData;
import org.sith.taskrunner.ejb.exceptions.EJBServiceExecutionException;
import org.sith.taskrunner.ejb.tasks.SumTask;
import org.sith.taskrunner.ejb.transformers.StringSumDataTransformer;


@EJB(name="SumTaskBean")
public class SumTaskBean implements SumTaskServiceLocal, SumTaskServiceRemote,
		Serializable {

	private String data;

	@Override
	public int getResult() throws EJBServiceExecutionException {
		int result = 0;
		try{
			Task sumTask = new SumTask();
			StringSumDataTransformer transformer = new StringSumDataTransformer();
			TaskInputData inputData = transformer.transform(data);
			sumTask.init(inputData);
			TaskExecutionResult<Integer> taskResult = sumTask.executeTask();
			result = taskResult.getResult();
		}catch(Exception e){
			throw new EJBServiceExecutionException(e);
		}		
		return result;
	}

	@Override
	public void initBean(String data) {
		if (data == null) {
			throw new NullPointerException("Input data for SumTask is null.");
		}
		this.data = data;
	}

}
